package domain.tournament;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;

import application.config.Config;
import application.config.Config.Status;
import domain.match.GameMatch;
import domain.match.IGameMatch;
import domain.users.User;

public class Tournament extends Observable implements ITournament, Serializable {

	private static final long serialVersionUID = -2403636607652216324L;
	private String title;
	private List<IGameMatch> matches;
	private List<User> players;
	private User leagueOwner;
	private User advertiser;
	private int tournamentID;
	private int numPlayers;

	public Tournament(int tournamentID, String title, User leagueOwner, int numPlayers) {
		this.leagueOwner = leagueOwner;
		this.tournamentID = tournamentID;
		this.matches = new ArrayList<IGameMatch>();
		this.players = new ArrayList<User>();
		this.numPlayers = numPlayers;
		this.title = title;
		

		IGameMatch gameMatch = new GameMatch(matches.size(), "The Ultimate Duel", 8080);
		gameMatch.addPlayer(new User(1, "Magnus", "derp", "Player"));
		gameMatch.addPlayer(new User(2, "Matte", "Awesome", "Player"));
		gameMatch.addPlayer(new User(3, "Robban", "derp", "Player"));
		matches.add(gameMatch);
		
		listMatchPlayers(0);
	}
	
	public List<IGameMatch> getMatches() {
		return matches;
	}	

	public void setTitle(String title) {
		this.title = title;
	}

	@Override
	public String getTitle() {
		return title;
	}

	@Override
	public void startGame() {
		System.out.println("Tournament.startGame()");
		notifyClients(Config.Status.GAME_STARTED);
	}

	@Override
	public void endGame() {
		System.out.println("Tournament.endGame()");
		notifyClients(Config.Status.GAME_ENDED);
	}

	@Override
	public User getWinner() {
		System.out.println("Tournament.getWinner()");
		notifyClients(Config.Status.MATCH_WON);

		return null;
	}

	@Override
	public void gameStarted() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void gameEnded() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void gameAborted() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void playerConnected(User player) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void playerDisconnected(User player) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	private void notifyClients(Status status) {
		setChanged();
		notifyObservers(status);
	}

	private void listMatchPlayers(int index) {
		IGameMatch match = matches.get(index);
		Iterator<User> iter = match.getPlayers();
		int i = 1;

		System.out.println("Listing players on match " + match);
		while (iter.hasNext()) {
			User player = (User) iter.next();
			System.out.println("player #" + i + " = " + player);
			i++;
		}
	}

	@Override
	public String toString() {
		return title + " (matches: " + matches.size() + ")";
	}

	@Override
	public void setAdvertiser(User advertiser) {
		this.advertiser = advertiser;
		
	}

	@Override
	public int getTournamentID() {
		return this.tournamentID;
	}

	@Override
	public int getNumPlayers() {
		return numPlayers;
	}

	@Override
	public void addPlayer(User player) {
		players.add(player);		
	}

}